import { createSSRApp } from 'vue'
//@ts-ignore
import uView from './uni_modules/vk-uview-ui'
import { $showToast } from '@/utils/msg'
import App from './App.vue'
import store from './store/global'
import { useAmapKeyStore } from './store/modules/amapKey'
import { doGetAmapKey, getLocation, locToAddress } from './apis/amap'

const amapKeyStore = useAmapKeyStore(store)
doGetAmapKey()
  .then((res) => {
    console.log(res)
    if (res.data) {
      amapKeyStore.SET_ALL_KEY(res.data)
    }
  })
  .catch((err) => {
    console.log('获取远程高德配置失败,使用本地静态配置', err)
    amapKeyStore.SET_AMAP_KEY()
    amapKeyStore.SET_AMAP_SAFE_CODE_PROXY(import.meta.env.VITE_GD_MAP_SAFECODE)
    amapKeyStore.SET_AMAP_WEB_SERVICE_KEY()
  })
  .finally(async () => {
    const lb = await getLocation()
    locToAddress([lb.longitude, lb.latitude])
  })

uni.$msg = $showToast
export function createApp() {
  const app = createSSRApp(App)
  app.use(store).use(uView)
  return {
    app,
  }
}
